﻿function expandCollapse(e) {
    if ($(e.item).is(".k-state-active")) {
        var that = this;
        window.setTimeout(function () { that.collapse(e.item); }, 1);
    } else {
        $('html, body').animate({
            scrollTop: $(e.item).offset().top - 100
        }, 100);
    }
}

function setuptabs(groupname) {
    //This code just allows us to select or unselect the tabs
    //Apply this to a group of tabs by name
    $("#"+groupname + " .tab-radio").off("click");
    $("#" + groupname + " .tab-radio").on("click", function () {
        //Need an internal data flag to keep track to unselect
        if ($(this).data("selected") === true) {
            $(this).data("selected", false);
            $(this).prop("checked", false);
        } else {
            $(this).data("selected", true);
            $(this).prop("checked", true);
            // Load Data - but only once.
            if ($(this).data("loadedonce") == undefined || $(this).data("loadedonce") == false)
            {
                if ($(this).data("divname") != undefined) {
                    toggledetails($(this).data("divname"), $(this).data("loadname"));
                }
                if ($(this).data("itemcount") == 1) {
                    $("#" + $(this).data("divzero")).click();
                    $('label[for="' + $(this).data("divzero") + '"]').hide();
                }
            }
            $(this).data("loadedonce", true);
        }
        //Then be sure to set that for ALL tabs.
        $("#" + groupname + " .tab-radio").each(function () {
            $(this).data("selected", $(this).prop("checked"));
        });
        // This will automatically open up the first tab if there is only one tab
        

        // scroll the tab to the top of the screen. Especially helps on mobile
        $(window).scrollTop($(this).position().top);
    });
}

function toggledetails(divname, loadname) {
    $("#" + divname).load(loadname);
}



function mergeandsizetabs(groupname) {
    $("#" + groupname + " .tab.inner").unwrap();
    $("#" + groupname + " .tab.inner").removeClass("inner");
    //resize tabs:
    var n = $("#" + groupname + ".tabs > .tab").length;
    var w = (($("#" + groupname + ".tabs").width()*.8) / n);
    if ($('body').width() > 600)
        $("#" + groupname + ".tabs >.tab > .tab-label").width(w);
}


